package com.yichi;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.yichi.global.EventDef;
import com.yichi.service.DeviceService;
import com.yichi.widget.VerifyEditText;

import java.util.Map;

public class EnterPasswordFragment extends Fragment {

    private final static String TAG = "EnterPasswordFragment";

    private VerifyEditText verifyEditText;
    private TextView errTip, tv_title;
    private TextView pwdBtnBack;
    private String userName;
    private String currPwd;//当前密码

    public EnterPasswordFragment(String userName, String pwd) {
        this.userName = userName;
        this.currPwd = pwd;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_enter_password, container, false);
        findView(view);
        setView();
        return view;
    }

    private void setView() {
        tv_title.setText(String.format("管理员【%s】正在处理，请输入密码", userName));
        verifyEditText.addInputCompleteListener(new VerifyEditText.InputCompleteListener() {
            @Override
            public void complete(String content) {
                if (content.equals(currPwd)) {
                    //密码正确
                    verifyEditText.setTextBackground(R.drawable.shape_verify_edittext_default_bg);
                    errTip.setVisibility(View.GONE);
                    Log.i(TAG, "密码正确");
                    LiveEventBus.get(EventDef.EVENT_OPSUSER_PWD_VERRIFY_PASSED, String.class).post("");
                } else {
                    //密码错误
                    verifyEditText.setTextBackground(R.drawable.shape_verify_edittext_error_bg);
                    errTip.setVisibility(View.VISIBLE);
                }
            }
        });
        pwdBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //返回首页
                MainActivity mainActivity = (MainActivity) requireActivity();
                mainActivity.goHome();
                //返回首页
//                Fragment adFragment = new AdFragment();
//                Fragment loginFragment = new LoginFragment();
//                getParentFragmentManager().beginTransaction().replace(R.id.ad_fragment_container, adFragment).commit();
//                getParentFragmentManager().beginTransaction().replace(R.id.login_fragment_container, loginFragment).commit();
            }
        });
    }

    private void findView(View view) {
        verifyEditText = view.findViewById(R.id.verify_edit_text);
        errTip = view.findViewById(R.id.err_tip);
        pwdBtnBack = view.findViewById(R.id.pwd_btn_back);
        tv_title = view.findViewById(R.id.tv_title);
    }
}
